//
//  CircularProgressView.m
//  CareRingApp
//
//  Created  on 2025/2/13.
//  
    

#import "CircularProgressView.h"
#import <Masonry/Masonry.h>
#import "ConfigModel.h"
@interface CircularProgressView()
@property (nonatomic, strong) CAShapeLayer *trackLayer;
@property (nonatomic, strong) CAShapeLayer *progressLayer;
//@property (nonatomic, strong) CALayer *startPointLayer;
@property (nonatomic, strong) CALayer *endPointLayer;
@end

@implementation CircularProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    self.midImageView = [UIImageView new];
    self.midImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    // 默认值设置
    self.progressColor = [UIColor blueColor];
    self.trackColor = [UIColor lightGrayColor];
    self.lineWidth = 10.0;
    self.progress = 0.0;
    
    // 创建轨道层
    self.trackLayer = [CAShapeLayer layer];
    self.trackLayer.fillColor = [UIColor clearColor].CGColor;
    self.trackLayer.strokeColor = self.trackColor.CGColor;
    self.trackLayer.lineWidth = self.lineWidth;
    self.trackLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:self.trackLayer];
    
    // 创建进度层
    self.progressLayer = [CAShapeLayer layer];
    self.progressLayer.fillColor = [UIColor clearColor].CGColor;
    self.progressLayer.strokeColor = self.progressColor.CGColor;
    self.progressLayer.lineWidth = self.lineWidth;
    self.progressLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:self.progressLayer];
    
    // 创建起点端点
//    self.startPointLayer = [CALayer layer];
//    self.startPointLayer.backgroundColor = self.startCapColor.CGColor;
//    [self.layer addSublayer:self.startPointLayer];
    
    // 创建终点端点
    self.endPointLayer = [CALayer layer];
    self.endPointLayer.backgroundColor = self.progressColor.CGColor;
    [self.layer addSublayer:self.endPointLayer];
    
    // 创建百分比标签
    self.percentageLabel = [[UILabel alloc] init];
    self.percentageLabel.textAlignment = NSTextAlignmentCenter;
    self.percentageLabel.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:28.0f];
    [self addSubview:self.percentageLabel];
    
    [self addSubview:self.self.midImageView];
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))/2 - self.lineWidth/2;
    
    // 创建圆形路径
//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center
//                                                             radius:radius
//                                                         startAngle:-M_PI_2
//                                                           endAngle:3*M_PI_2
//                                                          clockwise:YES];
  
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center
                                                             radius:radius
                                                          startAngle:-M_PI_2
                                                           endAngle:3*M_PI_2
                                                          clockwise:YES];
    self.trackLayer.path = circlePath.CGPath;
    self.progressLayer.path = circlePath.CGPath;
    
    // 设置端点大小
    CGFloat pointSize = self.lineWidth * 1.2;
//    self.startPointLayer.frame = CGRectMake(0, 0, pointSize, pointSize);
//    self.startPointLayer.cornerRadius = pointSize/2;
    self.endPointLayer.borderColor = self.progressColor.CGColor;
    self.endPointLayer.borderWidth = 3.0f;
    self.endPointLayer.frame = CGRectMake(0, 0, pointSize, pointSize);
    self.endPointLayer.cornerRadius = pointSize/2;
    
    // 更新端点位置
    [self updatePointsPosition];
    
    // 设置百分比标签位置
    [self.percentageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.percentageLabel.superview.mas_centerX);
        make.bottom.equalTo(self.percentageLabel.superview).inset(30);
        make.top.equalTo(self.percentageLabel.superview.mas_centerY).offset(3);
    }];
    
    [self.midImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.midImageView.superview.mas_centerX);
        make.bottom.equalTo(self.midImageView.superview.mas_centerY).inset(5);
        make.width.equalTo(self.midImageView.mas_height);
        make.height.equalTo(self.midImageView.superview.mas_height).multipliedBy(0.3);
    }];
    
//    self.midImageView.backgroundColor = UIColor.greenColor;
    
    
}

- (void)updatePointsPosition {
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))/2 - self.lineWidth/2;
    
    // 起点位置（固定在顶部）
//    self.startPointLayer.position = CGPointMake(center.x, center.y - radius);
    
    // 计算终点位置
    CGFloat startAngle = 0; // -M_PI_2
    CGFloat endAngle = (startAngle + (2 * M_PI * self.progress));
    CGFloat endX = center.x + radius * sin(endAngle);
    CGFloat endY = center.y - radius * cos(endAngle);
    self.endPointLayer.position = CGPointMake(endX, endY);
}

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
    // 确保进度值在0-1之间
    progress = MIN(1.0, MAX(0.0, progress));
    _progress = progress;
    
    // 更新进度层
    self.progressLayer.strokeEnd = progress;
    
    // 更新百分比标签
    if (progress * 100 >= 1) {
        self.percentageLabel.text = [NSString stringWithFormat:@"%.0f%%", progress * 100];
    } else {
        self.percentageLabel.text = nil;
    }
    // 更新端点位置
    if (animated) {
        [CATransaction begin];
        [CATransaction setAnimationDuration:0.3];
        [self updatePointsPosition];
        [CATransaction commit];
    } else {
        [self updatePointsPosition];
    }
}

#pragma mark - Setters

- (void)setProgressColor:(UIColor *)progressColor {
    _progressColor = progressColor;
    self.progressLayer.strokeColor = progressColor.CGColor;
//    self.startPointLayer.backgroundColor = progressColor.CGColor;
    self.endPointLayer.backgroundColor = progressColor.CGColor;
}

-(void)setStartCapColor:(UIColor *)startCapColor
{
    _startCapColor = startCapColor;
    self.endPointLayer.backgroundColor = startCapColor.CGColor;

}

- (void)setTrackColor:(UIColor *)trackColor {
    _trackColor = trackColor;
    self.trackLayer.strokeColor = trackColor.CGColor;
}

- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    self.trackLayer.lineWidth = lineWidth;
    self.progressLayer.lineWidth = lineWidth;
    [self setNeedsLayout];
}

-(void)reset
{
    [self setProgress:0 animated:NO];
    
}


@end
